const axios = require('axios');


const appID = 'wxecc9322469a3f41b';
const appSecret = '7803a7c5261cd59ce2fcc64b7d212b4f';

async function getAccessToken() {
  try {
    const response = await axios.get(`https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=${appID}&secret=${appSecret}`);
    console.log('response.data', response.data)
    const accessToken = response.data.access_token;
  } catch (error) {
    console.error('Error getting access token:', error.message);
    return null;
  }
}

async function sendGroupMessage() {
  
    const accessToken = await getAccessToken();
    console.log(accessToken)
    if (!accessToken) {
      return;
    }

    const message = {
      filter: {
        is_to_all: true // 群发给所有用户
      },
      text: {
        content: '我祈祷拥有一个透明的心灵和会流泪的眼睛'
      },
      msgtype: 'text'
    };

    const response = await axios.post(`https://api.weixin.qq.com/cgi-bin/message/mass/sendall?access_token=${accessToken}`, message);
    console.log(response.data)
    if (response.data.errcode === 0) {
      console.log('Group message sent successfully.');
    } else {
      console.log('Failed to send group message.');
    }
  
}

sendGroupMessage();
